package com.project.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.demo.controller.base.BaseController;
import com.project.demo.entity.RoomCleaning;
import com.project.demo.service.RoomCleaningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;


/**
 * 房间打扫：(RoomCleaning)表控制层
 *
 */
@RestController
@RequestMapping("/room_cleaning")
@Slf4j
public class RoomCleaningController extends BaseController<RoomCleaning, RoomCleaningService> {
    @Resource
    private RoomCleaningService roomCleaningService;

    /**
     * 房间打扫对象
     */
    @Autowired
    public RoomCleaningController(RoomCleaningService service) {
        setService(service);
    }


    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        return success(1);
    }

    @PostMapping("/upload/img")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file,
                                      @RequestParam("room_cleaning_id") String room_cleaning_id) {
        log.info("进入方法");
        if (file.isEmpty()) {
            return error(30000, "没有选择文件");
        }
        try {
            //判断有没路径，没有则创建
            String filePath = System.getProperty("user.dir") + "/src/main/resources/static/";
            File targetDir = new File(filePath);
            if (!targetDir.exists() && !targetDir.isDirectory()) {
                if (targetDir.mkdirs()) {
                    log.info("创建目录成功");
                } else {
                    log.error("创建目录失败");
                }
            }
//            String path = ResourceUtils.getURL("classpath:").getPath() + "static/upload/";
//            String filePath = path.replace('/', '\\').substring(1, path.length());
            String fileName = file.getOriginalFilename();
            File dest = new File(filePath + fileName);
            log.info("文件路径:{}", dest.getPath());
            log.info("文件名:{}", dest.getName());
            file.transferTo(dest);
            JSONObject jsonObject = new JSONObject();
            String url = "/api/upload/" + fileName;
            roomCleaningService.updateBaseSql(
                    "update room_cleaning set img = '" + url + "' where room_cleaning_id = " + room_cleaning_id);

            jsonObject.put("url", url);
            return success(jsonObject);
        } catch (IOException e) {
            log.info("上传失败：{}", e.getMessage());
        }
        return error(30000, "上传失败");
    }
}
